<template>
  <div>
    <div style="margin-bottom: 5px;">
      <el-input v-model="name" placeholder="请输入姓名" suffix-icon="el-icon-search" style="width: 200px;"
                @keyup.enter.native="loadPost"></el-input>
      <el-select v-model="isadmin" filterable placeholder="请选择性别" style="margin-left: 5px;">
        <el-option
            v-for="item in isadmins"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      <el-button style="margin-left: 5px;" type="primary" @click="loadPost">查询</el-button>
      <el-button style="margin-left: 5px;" type="success" @click="resetParam">重置</el-button>

      <el-button style="margin-left: 5px;" type="primary" @click="add">新增</el-button>


    </div>
  <el-table :data="tableData"
            :header-cell-style="{ background:'#f2f5fc',color:'#555555' }"
            border
  >
    <el-table-column prop="id" label="id" width="60">
    </el-table-column>
    <el-table-column prop="username" label="username" width="120">
    </el-table-column>
    <el-table-column prop="password" label="passeord" width="120">
    </el-table-column>
    <el-table-column prop="name" label="姓名" width="120">
    </el-table-column>
    <el-table-column prop="address" label="地址" width="300">
    </el-table-column>
    <el-table-column prop="phone" label="电话" width="120">
    </el-table-column>
    <el-table-column prop="isadmin" label="isadmin" width="120">
      <template slot-scope="scope">
        <el-tag
            :type="scope.row.isadmin === '1' ? 'primary' : 'success'"
            disable-transitions>{{scope.row.isadmin === '1' ?  '管理员' : '用户'}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="operate" label="操作" width="200">
      <template slot-scope="scope">
      <el-button size="mini" type="success" @click="mod(scope.row)">编辑</el-button>
        <el-popconfirm
            title="确定删除?"
            @confirm="del(scope.row.id)"
            style="margin-left: 5px;"
        >
          <el-button slot="reference" size="mini" type="danger" >删除</el-button>
        </el-popconfirm>
      </template>
<!--      <template slot-scope="scope">-->
<!--        <el-button-->
<!--            size="mini"-->
<!--            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
<!--        <el-button-->
<!--            size="mini"-->
<!--            type="danger"-->
<!--            @click="handleDelete(scope.$index, scope.row)">删除</el-button>-->
<!--      </template>-->
    </el-table-column>
  </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        title="提示"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>

      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-col :span="15">
          <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-col :span="15">
            <el-input v-model="form.username"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-col :span="15">
            <el-input v-model="form.password"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-col :span="15">
            <el-input v-model="form.address" ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-col :span="15">
            <el-input v-model="form.phone"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "MainView",
  data() {
    let checkDuplicate=(rule, value, callback) => {
      if (this.form.id){
        return callback();
      }
      this.$axios.get(this.$httpUrl +'/user/findByUn?username='+this.form.username).then(res=>res.data).then(res=>{
        if (res.code !==200){
          callback()
        }else {
          callback(new Error("账号已存在"))
        }
      })
    };

    return {
      tableData:[],
      pageSize:10,
      pageNum:1,
      total:0,
      name:'',
      isadmin:'',
      isadmins:[
        {
          value: '1',
          label: '管理员'
        }, {
          value: '0',
          label: '用户'
        }
      ],
      centerDialogVisible:false,
      form:{
        id:'',
        name:'',
        username:'',
        password:'',
        address:'',
        phone:'',
        roleId:'1'
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 5 个字符', trigger: 'blur' },
          {validator:checkDuplicate,trigger: 'blur'}
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ]

      }
    }
  },
  methods:{
    resetForm() {
      this.$refs.form.resetFields();
    },
    doMod(){
      this.$axios.post(this.$httpUrl+'/user/update',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){

          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible = false
          this.loadPost()
          this.resetForm()
        }else {
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    doSave(){
      this.$axios.post(this.$httpUrl+'/user/save',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible = false
          this.loadPost()
          this.resetForm()
        }else {
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    save(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id){
            this.doMod();
          }else {
            this.doSave();
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //删除
    del(id){
      console.log(id)

      this.$axios.get(this.$httpUrl+'/user/del?id='+id).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){

          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
      })


    },
    //修改
    mod(row){
      console.log(row)

      this.centerDialogVisible = true
      this.$nextTick(()=>{
        //赋值到表单
        this.form.id = row.id
        this.form.name = row.name
        this.form.username = row.username
        this.form.password =row.password
        this.form.address = row.address
        this.form.phone = row.phone
        this.form.roleId = row.roleId
      })

    },
    //添加
    add(){
      this.centerDialogVisible = true
      this.$nextTick(()=>{
        this.resetForm()
      })



    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    loadGet(){
      this.$axios.get(this.$httpUrl+'/user/list').then(res=>res.data).then(res=>{
        console.log(res)
      })
    },
    resetParam(){
      this.name=''
      this.isadmin=''
    },
    loadPost(){
      this.$axios.post(this.$httpUrl+'/user/listPageR',{
        pageSize:this.pageSize,
        pageNum:this.pageNum,
        param:{
          name:this.name,
          isadmin:this.isadmin

        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else ('获取数据失败')

      })
    }
  },
  beforeMount() {
    //this.loadGet()
    this.loadPost()
  }
}
</script>

<style scoped>

</style>